﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Xml;
using System.Xml.XPath;
using System.IO;

namespace ShowTime
{
    public partial class Default : System.Web.UI.Page
    {
        
        [WebMethod]
        public static string GetPinCollection()
        {
            string filename = Path.Combine(new[] { AppDomain.CurrentDomain.BaseDirectory, "App_Data", "PinData.xml" });
            XmlDocument doc = new XmlDocument();
            doc.Load( filename );
            string retString = "";

            XmlNodeList pinNodes = doc.SelectNodes("//Pub");

            foreach (XmlNode node in pinNodes)
            {
                for (int i = 0; i < node.Attributes.Count; i++)
                {
                    retString += node.Attributes[i].Value + "~";
                }
                retString += "|";
            }
            return retString;

        }

        [WebMethod]
        public static string SavePushPin(string myString)
        {
            string filename = Path.Combine(new[] { AppDomain.CurrentDomain.BaseDirectory, "App_Data", "PinData.xml" });
            //Open xml file for writing
            XmlDocument doc = new XmlDocument();
            doc.Load( filename );

            XmlNode root = doc.DocumentElement;

            XmlNode newNode = doc.CreateElement("Pub");

            //check if node exists, if so update, else create new

            string[] args = myString.Split('~');
            int lastID = 0;

            //if args[0](UID) is undefined, add new pin, else update pin
            if (args[0] == "undefined")
            {

                //get last id
                XmlNode lastNode = root.SelectSingleNode("//Pub[last()]");
                if (lastNode != null)
                    lastID = Convert.ToInt32(lastNode.Attributes[0].Value) + 1;

                XmlAttribute pubID = doc.CreateAttribute("id");
                pubID.Value = lastID.ToString();
                newNode.Attributes.Append(pubID);

                XmlAttribute pubName = doc.CreateAttribute("name");
                pubName.Value = args[1];
                newNode.Attributes.Append(pubName);

                XmlAttribute pubDescription = doc.CreateAttribute("description");
                pubDescription.Value = args[2];
                newNode.Attributes.Append(pubDescription);

                XmlAttribute pubRating = doc.CreateAttribute("rating");
                pubRating.Value = args[3];
                newNode.Attributes.Append(pubRating);


                XmlAttribute latitude = doc.CreateAttribute("latitude");
                latitude.Value = args[4];
                newNode.Attributes.Append(latitude);

                XmlAttribute longitude = doc.CreateAttribute("longitude");
                longitude.Value = args[5];
                newNode.Attributes.Append(longitude);

                //add new node to root
                root.AppendChild(newNode);
            }
            else
            {
                lastID = Convert.ToInt32(args[0]);
                XmlNode updateNode = doc.SelectSingleNode("//Pub[@id='" + args[0] + "']");
                for (int i = 0; i < args.Length; i++)
                    updateNode.Attributes[i].Value = args[i];
            }

            doc.Save(filename);
            return lastID.ToString();

        }
    }
}